//
//  DYRecommendConcernViewController.m
//  BaiSi
//
//  Created by mac on 16/11/11.
//  Copyright © 2016年 DeYi. All rights reserved.
//

#import "DYRecommendConcernViewController.h"
#import "DYLeftCell.h"
#import <AFNetworking/AFNetworking.h>
#import "DYLeftItem.h"
#import <MJRefresh/MJRefresh.h>
#import <MJExtension/MJExtension.h>
#import "DYRightItem.h"
#import "DYSubTagCell.h"

#define leftView_W  (DYScreenW * 0.3)
#define rightView_W  (DYScreenW * 0.7)
static NSString * const leftID = @"leftCell";
static NSString * const rightID = @"rightCell";

@interface DYRecommendConcernViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,weak) UITableView *leftTableView;
@property(nonatomic,weak) UITableView *rightTableView;
@property(nonatomic,weak) AFHTTPSessionManager *manager;
@property(nonatomic,strong) NSMutableArray *leftConcrens;
@property(nonatomic,weak) DYLeftItem *selectedItem;

@end

@implementation DYRecommendConcernViewController
#pragma mark - 懒加载
- (AFHTTPSessionManager *)manager {
    if (_manager == nil) {
        _manager = [AFHTTPSessionManager manager];
    }
    return _manager;
}

- (void)setSelectedItem:(DYLeftItem *)selectedItem {
    _selectedItem = selectedItem;
    if (_selectedItem.rightConcerns)
    {
        [_rightTableView reloadData];
        _rightTableView.mj_footer.hidden = _selectedItem.page > _selectedItem.total_page;
        return;
    }
        [_rightTableView.mj_header beginRefreshing];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"推荐关注";
    self.automaticallyAdjustsScrollViewInsets =  NO;
    
    //设置左边tableView
    [self setUpLeftTableView];
    
    //设置右边tableView
    [self setUpRightTableView];
    
    //加载左边数据
    [self loadLeftData];
    
    //设置上下拉
    [self setUpRefreshView];
    
}

#pragma mark - 集成上拉,下拉功能
- (void)setUpRefreshView {
    //下拉刷新
    MJRefreshNormalHeader *headerView = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadRightNewData)];
    _rightTableView.mj_header = headerView;
    headerView.automaticallyChangeAlpha = YES;
    
    //上拉加载
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadRightMoreData)];
    _rightTableView.mj_footer = footer;
    footer.automaticallyHidden = YES;
    
}

#pragma mark - 加载数据
- (void)loadLeftData
{
   
    NSMutableDictionary *paramters = [NSMutableDictionary dictionary];
    paramters[@"a"] = @"category";
    paramters[@"c"] = @"subscribe";
   
    [self.manager GET:DYBaseUrl parameters:paramters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
     

        self.leftConcrens = [DYLeftItem mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        
        //刷新表格
        [_leftTableView reloadData];
        
        //默认选中第一行
        NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
        [_leftTableView selectRowAtIndexPath:index animated:YES scrollPosition:UITableViewScrollPositionNone];
        DYLeftCell *cell = [_leftTableView cellForRowAtIndexPath:index];
        self.selectedItem = cell.item;
        
 
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            LHLog(@"%@",error);
        }
    }];
}

- (void)loadRightNewData
{
     _selectedItem.page = 1;
    //取消下拉加载
    [self.manager.tasks makeObjectsPerformSelector:@selector(cancel)];
    NSMutableDictionary *paramters = [NSMutableDictionary dictionary];
    paramters[@"a"] = @"list";
    paramters[@"c"] = @"subscribe";
     paramters[@"category_id"] = _selectedItem.ID;
    [self.manager GET:DYBaseUrl parameters:paramters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        _selectedItem.page++;
        //结束刷新
        [_rightTableView.mj_header endRefreshing];
        NSMutableArray *array = [DYRightItem mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        _selectedItem.rightConcerns = [NSMutableArray arrayWithArray:array];
        _selectedItem.total_page = [responseObject[@"total_page"] integerValue];

        //刷新表格
        [_rightTableView reloadData];
        //隐藏上拉
        _rightTableView.mj_footer.hidden = _selectedItem.page > _selectedItem.total_page;
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         //结束刷新
        [_rightTableView.mj_header endRefreshing];
        if (error) {
            LHLog(@"%@",error);
        }
    }];
}

- (void)loadRightMoreData
{
    //取消上拉加载
    [self.manager.tasks makeObjectsPerformSelector:@selector(cancel)];
    NSMutableDictionary *paramters = [NSMutableDictionary dictionary];
    paramters[@"a"] = @"list";
    paramters[@"c"] = @"subscribe";
    paramters[@"category_id"] = _selectedItem.ID;
    paramters[@"page"] = @(_selectedItem.page);
    [self.manager GET:DYBaseUrl parameters:paramters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        _selectedItem.page++;
        //结束刷新
        [_rightTableView.mj_footer endRefreshing];
        
        //转模型
        NSMutableArray *array = [DYRightItem mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        [_selectedItem.rightConcerns addObjectsFromArray:array];
        
        //刷新表格
        [_rightTableView reloadData];
        
        _rightTableView.mj_footer.hidden = _selectedItem.page > _selectedItem.total_page;
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //结束刷新
        [_rightTableView.mj_footer endRefreshing];
        if (error) {
            LHLog(@"%@",error);
        }
    }];
}

#pragma mark - 设置左边tableView

- (void)setUpLeftTableView {
    UITableView *leftView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, leftView_W, DYScreenH) style:UITableViewStylePlain];
    [self.view addSubview:leftView];
    _leftTableView = leftView;
    leftView.dataSource = self;
    leftView.delegate = self;
    leftView.backgroundColor = LHColor(215, 215, 215);
    //注册左边cell
    [_leftTableView registerNib:[UINib nibWithNibName:@"DYLeftCell" bundle:nil] forCellReuseIdentifier:leftID];
    //内边距
    leftView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    leftView.separatorStyle = UITableViewCellSeparatorStyleNone;
    leftView.tableFooterView = [[UIView alloc] init];
}

#pragma mark - 设置右边tableView
- (void)setUpRightTableView {
    UITableView *rightView = [[UITableView alloc] initWithFrame:CGRectMake(leftView_W, 0, rightView_W, DYScreenH) style:UITableViewStylePlain];
    [self.view addSubview:rightView];
    _rightTableView = rightView;
    rightView.dataSource = self;
    rightView.delegate = self;
    rightView.backgroundColor = LHColor(215, 215, 215);
    //注册右边cell
    [_rightTableView registerNib:[UINib nibWithNibName:@"DYSubTagCell" bundle:nil] forCellReuseIdentifier:rightID];
    //内边距
    rightView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    rightView.scrollIndicatorInsets = rightView.contentInset;
    rightView.separatorStyle = UITableViewCellSeparatorStyleNone;
    rightView.tableFooterView = [[UIView alloc] init];
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _leftTableView)
    {

        self.selectedItem = _leftConcrens[indexPath.row];
        

    }
    
}



#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _leftTableView) {//左边
        return _leftConcrens.count;
    }
    return _selectedItem.rightConcerns.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _leftTableView) {//左边
        DYLeftCell *leftCell = [tableView dequeueReusableCellWithIdentifier:leftID];
        leftCell.item = _leftConcrens[indexPath.row];
        return leftCell;
    }
    //右边
     DYSubTagCell *rightCell = [tableView dequeueReusableCellWithIdentifier:rightID];
     rightCell.rightConcernItem = _selectedItem.rightConcerns[indexPath.row];
    return rightCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _leftTableView) {
        return 44 + 1;
    }
    return 60;
}

@end
